package com.shuai.AI.ai_app.readers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.markdown.MarkdownDocumentReader;
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

/** 恋爱大师应用文档加载器 */
@Component
@Slf4j
public class LoveAppDocumentReader {

  private final ResourcePatternResolver resourcePatternResolver;

  public LoveAppDocumentReader(ResourcePatternResolver resourcePatternResolver) {
    this.resourcePatternResolver = resourcePatternResolver;
  }

  /**
   * 加载多篇 Markdown 文档
   *
   * @return
   */
  public List<Document> loadMarkdowns() {
    List<Document> allDocuments = new ArrayList<>();
    try {
      Resource[] resources = resourcePatternResolver.getResources("classpath:document/*.md");
      for (Resource resource : resources) {
        String filename = resource.getFilename();
        // 提取文档倒数第 3 和第 2 个字作为标签
        String status = filename.substring(filename.length() - 6, filename.length() - 4);
        MarkdownDocumentReaderConfig config =
            MarkdownDocumentReaderConfig.builder()
                .withHorizontalRuleCreateDocument(true)
                .withIncludeCodeBlock(false)
                .withIncludeBlockquote(false)
                .withAdditionalMetadata("filename", filename)
                .withAdditionalMetadata("status", status)
                .build();
        MarkdownDocumentReader markdownDocumentReader =
            new MarkdownDocumentReader(resource, config);
        allDocuments.addAll(markdownDocumentReader.get());
      }
    } catch (IOException e) {
      log.error("Markdown 文档加载失败", e);
    }
    return allDocuments;
  }
}
